﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Framework.Utils
{
    /// <summary>
    /// 字符串工具
    /// </summary>
    public class StringUtils
    {
        /// <summary>
        /// 字符串转成byte
        /// </summary>
        public static byte[] StringToByte(string str, Encoding encoding)
        {
            byte[] byteArray = encoding.GetBytes(str);
            return byteArray;
        }

        /// <summary>
        /// 数据流转成字符串
        /// </summary>
        public static string ByteToString(byte[] data, Encoding encoding)
        {
            string str = encoding.GetString(data);
            return str;
        }

        /// <summary>
        /// 字符串转成byte
        /// </summary>
        public static byte[] StringToByte(string str)
        {
            return StringToByte(str, System.Text.Encoding.ASCII);
        }

        /// <summary>
        /// 数据流转成字符串
        /// </summary>
        public static string ByteToString(byte[] data)
        {
            return ByteToString(data, System.Text.Encoding.ASCII);
        }



        /// <summary>
        /// 首字母大写
        /// </summary>
        public static string ToTitleCase(string str)
        {
            // return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);

            return str.Substring(0, 1).ToUpper() + str.Substring(1);
        }

        /// <summary>
        /// 字符串截取
        /// </summary>
        public static T[][] SplitTo2<T>(string str, string regexA, string regexB)
        {
            if (string.IsNullOrEmpty(str)) { return new T[0][]; }
            try
            {
                string[] strs = str.Split(new string[] { regexA }, StringSplitOptions.RemoveEmptyEntries);
                T[][] rets = new T[strs.Length][];
                for (int i = 0; i < strs.Length; i++)
                {
                    rets[i] = SplitTo<T>(strs[i], regexB);
                    if (rets[i] == null)
                    {
                        return null; // 解析错误
                    }
                }
                return rets;
            }
            catch (Exception) { }
            return null;
        }


        /// <summary>
        /// 字符串截取
        /// </summary>
        public static T[] SplitTo<T>(string str, string regex)
        {
            // 空处理
            if (string.IsNullOrEmpty(str))
            {
                return new T[0];
            }
            try
            {
                Type type = typeof(T);
                // 字符串分割
                string[] strs = str.Split(new string[] { regex }, StringSplitOptions.RemoveEmptyEntries);
                T[] array = new T[strs.Length];
                for (int i = 0; i < strs.Length; i++)
                {
                    var value = strs[i].Trim(); // 字符串处理
                    array[i] = (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);
                }
                return array;
            }
            catch (Exception) { }
            return null;
        }

        /// <summary>
        /// 把数组转成字符串
        /// </summary>
        public static string ToString(Array array, string regex)
        {
            StringBuilder strBdr = new StringBuilder();
            int asize = (array != null) ? array.Length : 0;
            for (int i = 0; i < asize; i++)
            {
                if (i > 0) { strBdr.Append(regex); }
                object value = array.GetValue(i);
                strBdr.Append(value.ToString());
            }
            return strBdr.ToString();
        }

        /// <summary>
        /// 字符串是否为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [Obsolete("请使用源生方法 string.IsNullOrEmpty")]
        public static bool IsEmpty(string str)
        {
            return str == null || str.Length <= 0;
        }

        /// <summary>
        /// 自动输出字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(object obj)
        {
            if (obj == null)
            {
                return "null";
            }
            Type t = obj.GetType();
            //转化
            StringBuilder strBdr = new StringBuilder();
            strBdr.Append(t.Name);
            strBdr.Append("[");

            // 遍历属性数组
            int index = 0;
            PropertyInfo[] properties = t.GetProperties();
            foreach (PropertyInfo p in properties)
            {
                //检测是否要跳过
                ToStringSkipAttribute skipAttribute = p.GetCustomAttribute<ToStringSkipAttribute>();
                if (skipAttribute != null)
                {
                    continue; //跳过
                }
                // ,符号添加
                if (index++ > 0)
                {
                    strBdr.Append(", "); // 第一个不加,
                }
                // 获取对象
                object value = p.GetValue(obj);
                // 字符串输出
                strBdr.Append(p.Name);
                strBdr.Append("=");
                if (value is Array)
                {
                    Array array = (Array)value;
                    strBdr.Append(ToArrayString(array));   // 列表
                }
                else
                {
                    strBdr.Append(value); // 普通对象
                }
            }
            strBdr.Append("]");
            return strBdr.ToString();
        }

        /// <summary>
        /// 词典处理
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string ToString<V>(Dictionary<string, V> dic)
        {
            // 遍历处理
            StringBuilder strBdr = new StringBuilder();
            strBdr.Append("[");
            int index = 0;
            foreach (KeyValuePair<string, V> kv in dic)
            {
                // ,符号添加
                if (index++ > 0)
                {
                    strBdr.Append(", "); // 第一个不加,
                }
                // 获取对象
                object value = kv.Value;
                // 字符串输出
                strBdr.Append(kv.Key);
                strBdr.Append("=");
                if (value is Array)
                {
                    Array array = (Array)value;
                    strBdr.Append(ToArrayString(array));   // 列表
                }
                else
                {
                    strBdr.Append(value); // 普通对象
                }
            }
            strBdr.Append("]");
            return strBdr.ToString();
        }

        /// <summary>
        /// 数组转字符串显示
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        private static string ToArrayString(Array array)
        {
            int lsize = (array != null) ? array.Length : 0;
            if (lsize <= 0)
            {
                return "[]";
            }
            // 遍历处理
            StringBuilder strBdr = new StringBuilder();
            strBdr.Append("[");
            for (int i = 0; i < lsize; i++)
            {
                if (i > 0)
                {
                    strBdr.Append(", ");
                }
                // 获取数据对象
                object obj = array.GetValue(i);
                if (obj == null)
                {
                    strBdr.Append("null");
                    continue;
                }

                // 特殊结构再遍历
                if (obj is Array)
                {
                    strBdr.Append(ToString((Array)obj));
                    continue;
                }
                else if (obj is Dictionary<string, object>)
                {
                    strBdr.Append(ToString((Dictionary<string, object>)obj));
                    continue;
                }

                // 通用输出
                //strBdr.Append(ToString(obj));
                strBdr.Append(obj.ToString());
            }
            strBdr.Append("]");
            return strBdr.ToString();
        }

        /// <summary>
        /// 自动输出字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString(Array array)
        {
            return ToArrayString(array);
        }

        /// <summary>
        /// 自动输出字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToString<T>(List<T> list)
        {
            if (list == null) { return "null"; }
            return ToArrayString(list.ToArray<T>());
        }


        /// <summary>
        /// ToString跳过属性
        /// </summary>
        [AttributeUsage(AttributeTargets.Property)]
        public class ToStringSkipAttribute : Attribute
        {
        }
    }
}
